package cn.wh.xwm.hibernate.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import cn.wh.xwm.common.util.Constant;

/**
 * A data access object (DAO) providing persistence and search support for
 * Userinfo entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.wh.xwm.hibernate.dao.Userinfo
 * @author MyEclipse Persistence Tools
 */

public class UserinfoDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UserinfoDAO.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String SEX = "sex";
	public static final String TEL = "tel";
	public static final String ROLE = "role";
	public static final String VALID = "valid";

	public void save(Userinfo transientInstance) {
		log.debug("saving Userinfo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Userinfo persistentInstance) {
		log.debug("deleting Userinfo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void update(Userinfo persistentInstance) {
		log.debug("update Userinfo instance");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public Userinfo findById(java.lang.String id) {
		log.debug("getting Userinfo instance with id: " + id);
		try {
			Userinfo instance = (Userinfo) getSession().get(
					"cn.wh.xwm.hibernate.dao.Userinfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Userinfo instance) {
		log.debug("finding Userinfo instance by example");
		try {
			List results = getSession().createCriteria(
					"cn.wh.xwm.hibernate.dao.Userinfo").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public Userinfo findByUsernameAndPassword(Userinfo instance) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", instance.getUsername());
		map.put("password", instance.getPassword());
		try {
			List<Userinfo> results = getSession().createCriteria(
					"cn.wh.xwm.hibernate.dao.Userinfo").add(
					Restrictions.allEq(map)).list();
			log.debug("find by UsernameAndPassword, result size: "
					+ results.size());
			if (results.size() > 0) {
				return results.get(0);
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Userinfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Userinfo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findByRole(Object role) {
		return findByProperty(ROLE, role);
	}

	public List findByValid(Object valid) {
		return findByProperty(VALID, valid);
	}

	public List findAll() {
		log.debug("finding all Userinfo instances");
		try {
			String queryString = "from Userinfo where username<>'admin'";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Userinfo merge(Userinfo detachedInstance) {
		log.debug("merging Userinfo instance");
		try {
			Userinfo result = (Userinfo) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Userinfo instance) {
		log.debug("attaching dirty Userinfo instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Userinfo instance) {
		log.debug("attaching clean Userinfo instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public String findTotalBysql(String q_mobile, String q_username) {
		String presql = "select count(*) from snfims.userinfo where 1=1 ";
		if (q_mobile != null && !q_mobile.trim().equals("")) {
			presql = presql + "and tel like '%" + q_mobile + "%' ";
		}
		if (q_username != null && !q_username.trim().equals("")) {
			presql = presql + "and username like '%" + q_username + "%' ";
		}
		return this.findTotalBysql(presql);
	}

	public List<Userinfo> findByPrame(String page, String row, String q_mobile,
			String q_username) {
		String prehql = "from Userinfo as model where 1=1 ";
		if (q_mobile != null && !q_mobile.trim().equals("")) {
			prehql = prehql + "and model.tel like '%" + q_mobile + "%' ";
		}
		if (q_username != null && !q_username.trim().equals("")) {
			prehql = prehql + "and model.username like '%" + q_username + "%' ";
		}
		Query queryObject = getSession().createQuery(prehql);

		if (row == null || row.trim().equals("")) {
			row = Constant.ROWLIMIT;
		}
		if (page == null|| page.trim().equals("")) {
			page = Constant.FIRSTPAGE;
		}
		int ipage = Integer.valueOf(page);
		int irow = Integer.valueOf(row);
		int offset = (ipage - 1) * irow;
		queryObject.setFirstResult(offset);
		queryObject.setMaxResults(irow);
		return queryObject.list();
	}
}